package com.company.cloud.web.render;

import com.jfinal.template.Engine;

import javax.servlet.ServletContext;
import java.io.File;

public class RenderFactory implements IRenderFactory {

    protected Engine engine;
    protected Constants constants;
    protected ServletContext servletContext;
    protected MainRenderFactory mainRenderFactory;

    @Override
    public void init(Engine engine, Constants constants, ServletContext servletContext) {
        this.engine = engine;
        this.constants = constants;
        this.servletContext = servletContext;
        switch (constants.getViewType()) {
            case JFINAL_TEMPLATE:
                mainRenderFactory = new MainRenderFactory();
                break;
            case FREE_MARKER:
                mainRenderFactory = new FreeMarkerRenderFactory();
                break;
            case JSP:
                mainRenderFactory = new JspRenderFactory();
                break;
            case VELOCITY:
                mainRenderFactory = new VelocityRenderFactory();
                break;
        }


    }

    @Override
    public Render getRender(String view) {
        return mainRenderFactory.getRender(view);
    }

    @Override
    public Render getTemplateRender(String view) {
        return new TemplateRender(view);
    }

    @Override
    public Render getFreeMarkerRender(String view) {
        //todo:
        return null;
    }

    @Override
    public Render getJspRender(String view) {
        //todo:
        return null;
    }

    @Override
    public Render getVelocityRender(String view) {
        //todo:
        return null;
    }

    @Override
    public Render getJsonRender() {
        return new JsonRender();
    }

    @Override
    public Render getJsonRender(String key, Object value) {
        //todo:
        return null;
    }

    @Override
    public Render getJsonRender(String[] attrs) {
        //todo:
        return null;
    }

    @Override
    public Render getJsonRender(String jsonText) {
        //todo:
        return null;
    }

    @Override
    public Render getJsonRender(Object object) {
        //todo:
        return null;
    }

    @Override
    public Render getTextRender(String text) {
        return new TextRender(text);
    }

    @Override
    public Render getTextRender(String text, String contentType) {
        return new TextRender(text, contentType);
    }

    @Override
    public Render getTextRender(String text, ContentType contentType) {
        return new TextRender(text, contentType);
    }

    @Override
    public Render getDefaultRender(String view) {
        return getRender(view + constants.getViewExtension());
    }


    @Override
    public Render getErrorRender(int errorCode, String view) {
        //todo:
        return null;
    }

    @Override
    public Render getErrorRender(int errorCode) {
        //todo:
        return null;
    }

    @Override
    public Render getFileRender(String fileName) {
        return new FileRender(fileName);
    }

    @Override
    public Render getFileRender(String fileName, String downloadFileName) {
        return new FileRender(fileName, downloadFileName);
    }

    @Override
    public Render getFileRender(File file) {
        return new FileRender(file);
    }

    @Override
    public Render getFileRender(File file, String downloadFileName) {
        return new FileRender(file, downloadFileName);
    }

    @Override
    public Render getRedirectRender(String url) {
        //todo:
        return null;
    }

    @Override
    public Render getRedirectRender(String url, boolean withQueryString) {
        //todo:
        return null;
    }

    @Override
    public Render getRedirect301Render(String url) {
        //todo:
        return null;
    }

    @Override
    public Render getRedirect301Render(String url, boolean withQueryString) {
        //todo:
        return null;
    }

    @Override
    public Render getNullRender() {
        //todo:
        return null;
    }

    @Override
    public Render getJavascriptRender(String jsText) {
        //todo:
        return null;
    }

    @Override
    public Render getHtmlRender(String htmlText) {
        return new HtmlRender(htmlText);
    }

    @Override
    public Render getXmlRender(String xmlText) {
        return new XmlRender(xmlText);
    }

    @Override
    public Render getCaptchaRender() {
        //todo:
        return null;
    }

    @Override
    public Render getQrCodeRender(String content, int width, int height) {
        //todo:
        return null;
    }

    @Override
    public Render getQrCodeRender(String content, int width, int height, char errorCorrectionLevel) {
        //todo:
        return null;
    }


    // ----------------------------------------
    private static class MainRenderFactory {
        public Render getRender(String view) {
            return new TemplateRender(view);
        }
    }

    private static class FreeMarkerRenderFactory extends MainRenderFactory {
        public Render getRender(String view) {
//            return new FreeMarkerRender(view);
            return null;
        }
    }

    private static class JspRenderFactory extends MainRenderFactory {
        public Render getRender(String view) {
            return new JspRender(view);
        }
    }

    private static class VelocityRenderFactory extends MainRenderFactory {
        public Render getRender(String view) {
//            return new VelocityRender(view);
            return null;
        }
    }

}
